<?php
	class AdmTypeHebergement extends AdmModule {
		public function action_index() {
			if (isset($this->session->modification_type_hebergement_id)) {
				unset($this->session->modification_type_hebergement_id);
			}
			
			$this->set_title("Liste des type d'hébergement possibles");
			$this->tpl->assign('liste', TypeHebergement::recupererListe());
		}
		
		public function action_ajouter() {
			$formulaire = new Form("?module=admtypehebergement&action=valider_ajout","formulaire");
			$formulaire->add_text("type_hebergement","type_hebergement","Détails sur le type :");
			$formulaire->add_submit("ajouter_type_hebergement_submit", "ajouter_type_hebergement_submit")->set_value("Ajouter le type");	
			
			$this->tpl->assign("formulaire", $formulaire);		
		}
		
		public function action_valider_ajout() {
			if ($this->req->type_hebergement == '') {
				$this->site->ajouter_message("Veuillez indiquer le type.");
				$this->site->redirect('admtypehebergement', 'ajouter');
			}
			else {
				$type_hebergement = new TypeHebergement($this->req->type_hebergement);
				$type_hebergement->enregistrer();
				
				$this->site->redirect('admtypehebergement');
			}
		}
		
		public function action_modifier() {
			if ($this->req->id > 0 || $this->session->modification_type_hebergement_id > 0) {
				try {
					$type_hebergement = TypeHebergement::recupererAvecID($this->req->id);
				}
				catch (MyException $e) {
					try {
						$type_hebergement = TypeHebergement::recupererAvecID($this->session->modification_type_hebergement_id);
					}
					catch (MyException $e) {
					}
				}
				
				if (isset($this->session->modification_type_hebergement_id)) {
					$this->req->id = $this->session->modification_type_hebergement_id;
					unset($this->session->modification_type_hebergement_id);
				}
				
				$formulaire = new Form("?module=admtypehebergement&action=valider_modification","formulaire");
				$formulaire->add_text("type_hebergement","type_hebergement","Détails sur le type :")->set_value($type_hebergement->valeur);
				$formulaire->add_submit("modifier_type_hebergement_submit", "modifier_type_hebergement_submit")->set_value("Modifier le type");	
				$this->session->modification_type_hebergement_id = $this->req->id;
				
				$this->tpl->assign("formulaire", $formulaire);		
			}
			else {
				$this->site->redirect('admtypehebergement');
			}
		}
		
		public function action_valider_modification() {
			if ($this->session->modification_type_hebergement_id <= 0) {
				$this->site->redirect('admtypehebergement');
			}
			elseif ($this->req->type_hebergement == '') {
				$this->site->ajouter_message('Vous ne pouvez pas donner un type vide !');
				$this->site->redirect('admtypehebergement', 'modifier');
			}	
			else {
				
				try {
					$type_hebergement = TypeHebergement::recupererAvecID($this->session->modification_type_hebergement_id);
				}
				catch (MyException $e) {
				}
				
				unset($this->session->modification_type_hebergement_id);
				
				$type_hebergement->valeur = $this->req->type_hebergement;

				$type_hebergement->enregistrer();
				
				$this->site->ajouter_message('Modification effectuée');
				$this->site->redirect('admtypehebergement');
			}
		}
		
		public function action_supprimer() {
			if ($this->req->id > 0) {
				$type_hebergement = typehebergement::recupererAvecID($this->req->id);
				$type_hebergement->supprimer();
				
				$this->site->ajouter_message('Suppression effectuée');
				$this->site->redirect('admtypehebergement');
			}
			else {
				$this->site->redirect('admtypehebergement');
			}
		}
	}
?>